﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	public class InterChangeMultyDuctWH : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Document document = commandData.Application.ActiveUIDocument.Document;
			Selection selection = commandData.Application.ActiveUIDocument.Selection;
			Transaction transaction = new Transaction(document, "批量调换风管内部宽高值");
			int num = 0;
			try
			{
				transaction.Start();
				RevitVersionFuncs.ClearSelection(selection);
				DuctFilter ductFilter = new DuctFilter();
				IEnumerable<Reference> enumerable = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, ductFilter, "选择要调换宽高值的风管：");
				List<Element> list = new List<Element>();
				foreach (Reference reference in enumerable)
				{
					Duct duct = document.GetElement(reference) as Duct;
					if (duct != null)
					{
						list.Add(duct);
						if (new HYTagOperate().WHExchange(duct))
						{
							num++;
						}
					}
				}
				RevitVersionFuncs.SetSelection(selection, list);
                switch ((int)transaction.Commit())
				{
				case 2:
					YJKMessageBox.Show("风管宽高值互换失败！");
					return Autodesk.Revit.UI.Result.Failed;
				case 3:
					YJKMessageBox.Show("共有" + num + "条风管宽高值互换成功！");
					break;
				case 5:
					YJKMessageBox.Show("风管宽高值互换失败！");
					return Autodesk.Revit.UI.Result.Failed;
				case 6:
					YJKMessageBox.Show("共有" + num + "条风管宽高值互换成功！");
					break;
				}
			}
			catch (Exception)
			{
				transaction.Dispose();
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
